import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'function.dart';

class MonitorSetPage extends StatefulWidget {
  MonitorSetPage(this.mainColor);
  final mainColor;
  @override
  _MonitorSetPageState createState() => new _MonitorSetPageState();
}

class _MonitorSetPageState extends State<MonitorSetPage> {
  bool income = false;
  bool offline = false;
  bool exception = false;
  bool resume = false;
  bool autodraw = false;
  int repush = 20;
  int disk = 20;
  int weekday = 1;
  String uid = "";
  @override
  void initState() {
    super.initState();
    initMonitor();
  }
  save()async{
    Map<String,dynamic> data={};
    data['uid']=uid;
    data['income']=income?1:0;
    data['offline']=offline?1:0;
    data['exception']=exception?1:0;
    data['resume']=resume?1:0;
    data['autodraw']=autodraw?1:0;
    data['repush']=repush;
    data['disk']=disk;
    data['weekday']=weekday;
    var saveRes=await setMonitor(data);
    if(saveRes['code']==1){
      toast(saveRes['msg']);
    }else{
      toast(saveRes['msg']);
    }
  }
  initMonitor() async {
    this.uid = await getStorage("uid");
    var info = await getUserInfo(this.uid);
    print(info['msg']['weekday']);
    this.income = info['msg']['income'] == "1";
    this.offline = info['msg']['offline'] == "1";
    this.exception = info['msg']['exception'] == "1";
    this.resume = info['msg']['resume'] == "1";
    this.autodraw = info['msg']['autodraw'] == "1";
    this.repush = int.parse(info['msg']['repush']);
    this.disk = int.parse(info['msg']['disk']);
    this.weekday = int.parse(info['msg']['weekday']);
    print(income);
    print(info['msg']['income']);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new CupertinoNavigationBar(
        actionsForegroundColor: Colors.white,
        backgroundColor: widget.mainColor,
        middle: new Text(
          "监控设置",
          style: new TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.normal,
          ),
        ),
        trailing: new CupertinoButton(

          padding: EdgeInsets.zero,
          child:new Text("保存",style: new TextStyle(fontSize:16.0,color:Colors.white),),
          pressedOpacity: 0.8,
          onPressed: save,
          minSize: 24.0,
        ),
      ),
      body: new Container(
        child: new Column(
          children: <Widget>[
            new ListTile(
              title: new Text("离线提醒"),
              subtitle: new Text("当设备出现断电、掉网时提醒"),
              trailing: CupertinoSwitch(
                value: offline,
                onChanged: (val) async {
                  offline = val;
                  setState(() {});
                },
              ),
            ),
            new Divider(
              height: 1.0,
            ),
            new ListTile(
              title: new Text("异常提醒"),
              subtitle: new Text("当设备出现掉盘等异常时提醒"),
              trailing: CupertinoSwitch(
                value: exception,
                onChanged: (val) async {
                  exception = val;
                  setState(() {});
                },
              ),
            ),
            new Divider(
              height: 1.0,
            ),
            new ListTile(
              title: new Text("恢复提醒"),
              subtitle: new Text("当设备从离线、异常状态恢复时提醒"),
              trailing: CupertinoSwitch(
                value: resume,
                onChanged: (val) async {
                  resume = val;
                  setState(() {});
                },
              ),
            ),
            new Divider(
              height: 1.0,
            ),
            new ListTile(
              title: new Text("收入提醒"),
              subtitle: new Text("每天早上7天通知昨日收益"),
              trailing: CupertinoSwitch(
                value: income,
                onChanged: (val) async {
                  income = val;
                  setState(() {});
                },
              ),
            ),
            new Divider(
              height: 1.0,
            ),
            new ListTile(
              title: new Text("磁盘空间"),
              subtitle: new Text("当磁盘空间低于设定值时提醒"),
              trailing: DropdownButtonHideUnderline(
                child: new DropdownButton(
                  isDense: true,
                  value: disk,
                  onChanged: (value) {
                    setState(() {
                      disk = value;
                    });
                  },
                  items: [
                    new DropdownMenuItem<int>(
                      value: 0,
                      child: new Text("关闭"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 5,
                      child: new Text("5%"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 10,
                      child: new Text("10%"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 20,
                      child: new Text("20%"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 30,
                      child: new Text("30%"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 40,
                      child: new Text("40%"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 50,
                      child: new Text("50%"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 60,
                      child: new Text("60%"),
                    ),
                  ],
                ),
              ),
            ),
            new Divider(
              height: 1.0,
            ),
            new ListTile(
              title: new Text("自动提币"),
              subtitle: new Text("每个星期几10点10分自动提币"),
              trailing: new DropdownButtonHideUnderline(
                child: new DropdownButton(
                  isDense: true,
                  value: weekday,
                  onChanged: (value) {
                    setState(() {
                      weekday = value;
                    });
                  },
                  items: [
                    new DropdownMenuItem<int>(
                      value: 0,
                      child: new Text("关闭"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 1,
                      child: new Text("星期一"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 2,
                      child: new Text("星期二"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 3,
                      child: new Text("星期三"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 4,
                      child: new Text("星期四"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 5,
                      child: new Text("星期五"),
                    ),
                  ],
                ),
              ),
            ),
            new Divider(
              height: 1.0,
            ),
            new ListTile(
              title: new Text("再次提醒"),
              subtitle: new Text("设备持续异常再次提醒时间"),
              trailing: new DropdownButtonHideUnderline(
                child: new DropdownButton(
                  isDense: true,
                  value: repush,
                  onChanged: (value) {
                    setState(() {
                      repush = value;
                    });
                  },
                  items: [
                    new DropdownMenuItem<int>(
                      value: 0,
                      child: new Text("关闭"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 10,
                      child: new Text("10分钟"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 20,
                      child: new Text("20分钟"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 30,
                      child: new Text("30分钟"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 40,
                      child: new Text("40分钟"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 50,
                      child: new Text("50分钟"),
                    ),
                    new DropdownMenuItem<int>(
                      value: 60,
                      child: new Text("60分钟"),
                    ),
                  ],
                ),
              ),
            ),
            new Divider(
              height: 1.0,
            ),
          ],
        ),
      ),
    );
  }
}
